#!/usr/bin/python3
import sys
from collections import deque

# part 1

def shift(enc, pair):
    i = enc.index(pair)
    enc.rotate(-i)
    enc.popleft()
    enc.rotate(-pair[1])
    enc.append(pair)

def solution(key=1, iterations=1):
    inp = [int(line.strip()) * key for line in sys.stdin]
    orig = list(enumerate(inp))
    enc = deque(orig.copy())
    print(enc)
    for i in range(iterations):
        for pair in orig:
            shift(enc, pair)
        print(enc)
        index = [n for i, n in enc].index(0)
        length = len(enc)
    _, v1 = enc[(1000 + index) % length]
    _, v2 = enc[(2000 + index) % length]
    _, v3 = enc[(3000 + index) % length]
    print(v1, v2, v3, 'with sum', sum([v1, v2, v3]))

if sys.argv[1] in '1':
    solution()
else:
    solution(811589153, 10)
